package com.cat.model.web.action.admin.model;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.common.model.common.CompanyInfoVO;
import com.cat.model.common.model.common.ModelInfoVO;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class ModelPickupAction  extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;
	
	private Long id;
	private ModelInfoVO modelInfo;
	private String name; 
	private Long albumId;
	private Long solutionId;
	private Long companyId;
	private BigDecimal price;
	@Autowired
	private PlmCommDAO plmCommDAO;
	@Autowired
	private ModelMapper modelMapper;
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String pickup() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(id != null){
			condition.put("id", id);
		}
		
		if(StringUtils.isNotEmpty(name)){
			condition.put("name", name);
		}
		if(price!=null){
			condition.put("price", price);
		}
		if(companyId!=null){
			condition.put("companyId", companyId);
		}
		
		if(albumId != null){
			condition.put("albumId", albumId);
		}
		
		if(solutionId != null){
			condition.put("solutionId", solutionId);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchModelInfoSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<ModelInfoVO> list = (List<ModelInfoVO>)plmCommDAO.search("manager.searchModelInfo", condition, start, page.getNumPerPage());
		for (ModelInfoVO modelInfoVO : list) {
			CompanyInfoVO companyInfoVO=modelMapper.getCompanyInfo(modelInfoVO.getCompanyId());
			if(companyInfoVO!=null){
				modelInfoVO.setCompanyName(companyInfoVO.getName());
			}
			modelInfoVO.setCoverImage(ImageUtil.wrapImageUrl(modelInfoVO.getCoverImage()));
		}
		List<CompanyInfoVO> companyList = modelMapper.getAllCompany();
		request.setAttribute("companyList", companyList);
		request.setAttribute("companyId", companyId);
		request.setAttribute("price", price);
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "pickup";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ModelInfoVO getModelInfo() {
		return modelInfo;
	}

	public void setModelInfo(ModelInfoVO modelInfo) {
		this.modelInfo = modelInfo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getAlbumId() {
		return albumId;
	}

	public void setAlbumId(Long albumId) {
		this.albumId = albumId;
	}

	public Long getSolutionId() {
		return solutionId;
	}

	public void setSolutionId(Long solutionId) {
		this.solutionId = solutionId;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}
	

}
